import javax.sound.midi.MidiSystem;
import javax.sound.midi.Receiver;
import javax.sound.midi.ShortMessage;
import javax.sound.midi.Synthesizer;


public class Midi {

	public static void main(final String[] args) throws Exception {
		Synthesizer s = MidiSystem.getSynthesizer();
		s.open();
		Receiver r = s.getReceiver();
//		System.err.println(s);
//		System.err.println(r);
//		for (Object p : System.getProperties().keySet()) {
//			System.err.println(p + "\t" + System.getProperty((String) p));
//		}
		ShortMessage shortMessage;
		s.getChannels()[3].programChange(40);
		shortMessage = new ShortMessage();
		shortMessage.setMessage(ShortMessage.PROGRAM_CHANGE, 3, 40);
		r.send(shortMessage, 1);
		int last = 69;
		for (String st : args) {
			
			int i = Integer.parseInt(st); 
			int lower = (i == 0 ? last : i)-36;
			System.err.println(i + "\t("+ last+")\t" +lower);
			
			if (i != 0) {
				shortMessage = new ShortMessage();
				shortMessage.setMessage(ShortMessage.NOTE_ON, 1, i, 60);
				r.send(shortMessage, 1);
				last = i;
			}
			shortMessage = new ShortMessage();
			shortMessage.setMessage(ShortMessage.NOTE_ON, 3, lower, 60);
			r.send(shortMessage, 1);
			
			
			
			Thread.sleep(300);
			shortMessage = new ShortMessage();
			shortMessage.setMessage(ShortMessage.NOTE_OFF, 3, lower, 80);
			r.send(shortMessage, 1);
			Thread.sleep(200);
				
		}
		System.err.println("END");
	
		r.close();
		s.close();
		
	}
	
	
}
